package com.hjq.mallorder.config;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.convert.converter.Converter;

import javax.annotation.PostConstruct;

/**
 * @auther HJQ
 */

@Configuration
public class RabbitConfig {


    @Bean
    public MessageConverter converter() {
        return new Jackson2JsonMessageConverter();
    }

    @PostConstruct//对象创建玩后在调用这个方法
    public RabbitTemplate setRabbitTemplate() {
        RabbitTemplate rabbitTemplate=new RabbitTemplate();
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {//消息回调
            /**
             *
             * @param correlationData 当前消息的关联数据
             * @param b 消息是否成功收到
             * @param s 失败原因
             */
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {

            }
        });
        //设置消息抵达队列的抵达失败的回调
        rabbitTemplate.setReturnsCallback(returnedMessage -> {

        });
        return rabbitTemplate;
    }
}
